<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{URL::asset('layui/css/layui.css')}}"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form class="layui-form" action="">
    <fieldset class="layui-elem-field" style="margin-top: 20px;">
        <legend>基本信息</legend>
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="角色名称" class="layui-input">
            </div>
        </div>
    </fieldset>
    <fieldset class="layui-elem-field" style="margin-top: 20px;">
        <legend>角色授权</legend>
            @foreach($menu as $item)
                <fieldset class="layui-elem-field" style="margin-top: 20px;">
                    <legend><input type="checkbox" name="role" lay-skin="primary" value="{{$item['id']}}" title="{{$item['menu_name']}}"></legend>
                    <div class="layui-form-item" pane="">
                        <div class="layui-input-block">
                            @foreach($item['child'] as $value)
                                <input type="checkbox" name="role" lay-skin="primary" value="{{$value['id']}}" title="{{$value['menu_name']}}">
                            @endforeach
                        </div>
                    </div>
                </fieldset>
            @endforeach

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">取消</button>
                </div>
            </div>
    </fieldset>
</form>

<script src="{{URL::asset('layui/layui.js')}}" charset="utf-8"></script>
<script src="{{URL::asset('js/jquery-3.1.1.min.js')}}" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form'], function(){
        var form = layui.form
            ,layer = layui.layer;

        //监听提交
        form.on('submit(demo1)', function(data){
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            var value = '';
            $('input:checkbox[name="role"]:checked').each(function (i,v) {
                value += v.value+',';
            });
            var menu_id = value.substring(0,value.lastIndexOf(','));

            var url = "/admin/role/api/add";
            var where = {"name":data.field.name,"menu_id":menu_id};
            $.post(url,where,function (d) {
                if (d.code==200){
                    layer.msg("保存成功");
                    setTimeout(function () {
                        window.parent.location.reload();
                    },500);
                }else{
                    layer.msg("保存失败");
                }
            });
            return false;
        });
    });
</script>

</body>
</html>